package com.gemme.gemme.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.gemme.gemme.R;
import com.gemme.gemme.home.ShopProductInfoBean;
import com.gemme.gemme.wode.ProductNewActivity;

import java.util.List;

//首页水平滚动
public class NewProductView extends RelativeLayout {
    private Context context;
    public NewProductView(Context context) {
        this(context,null);
    }

    public NewProductView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public NewProductView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    ProductSingleView first;
    ProductSingleView second;
    ProductSingleView third;
    ProductSingleView four;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.new_product_view,this);
        first = findViewById(R.id.first);
        second = findViewById(R.id.second);
        third = findViewById(R.id.third);
        four = findViewById(R.id.four);

    }

    public void setDataList(List dataLsit) {
        try {
            List<ShopProductInfoBean.DataBean.NewObjBean> list = dataLsit;

            for (int i = 0; i < list.size(); i++) {
                ShopProductInfoBean.DataBean.NewObjBean newObjBean = list.get(i);
                switch (i){
                    case 0:
                        first.setVisibility(VISIBLE);
                        setSingleData(first,newObjBean);
                        break;
                    case 1:
                        second.setVisibility(VISIBLE);
                        setSingleData(second,newObjBean);
                        break;
                    case 2:
                        third.setVisibility(VISIBLE);
                        setSingleData(third,newObjBean);
                        break;
                    case 3:
                        four.setVisibility(VISIBLE);
                        setSingleData(four,newObjBean);
                        break;
                }
            }

        }catch (Exception e){

        }
    }

    private void setSingleData(ProductSingleView first, ShopProductInfoBean.DataBean.NewObjBean newObjBean) {
        try {
            first.setName(newObjBean.getProductName())
            .setPrice(newObjBean.getPrice() + "")
            .setPicture(newObjBean.getAbbreviationPic())
            .settv_desc(newObjBean.getProductDesc())
            ;


            first.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, ProductNewActivity.class);
                    intent.putExtra("id",newObjBean.getProductId());
                    intent.putExtra("brandId",newObjBean.getBrandId() + 0L);
                    context.startActivity(intent);
                }
            });
        }catch (Exception e){

        }
    }
}
